<template>
    <div>
        <el-form :model="Purchase" label-width="100px" :rules="rules" ref="UpdataApplyPurchase_refs" :disabled="IsSee">
            <el-row :gutter="10">
                <el-col :span="8">
                    <el-form-item label="采购部门" prop="DepartmentId">
                        <el-select placeholder="请选择" v-model="Purchase.DepartmentId" style="width: 100%" filterable>
                            <el-option v-for="v in AllDepartment" :label="v.DepartmentName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <!--                <el-col :span="8">-->
                <!--                    <el-form-item label="申购人员" prop="ApplyUserId">-->
                <!--                        <el-select placeholder="请选择" v-model="Purchase.ApplyUserId" style="width: 100%" filterable>-->
                <!--                            <el-option v-for="v in $store.state.AllUsers" v-if="v.InternshipState!=8" :label="v.UserName" :value="v.Id"></el-option>-->
                <!--                        </el-select>-->
                <!--                    </el-form-item>-->
                <!--                </el-col>-->
                <el-col :span="8">
                    <el-form-item label="采购人员" prop="PurchaseUserId">
                        <el-select placeholder="请选择" v-model="Purchase.PurchaseUserId" style="width: 100%" filterable>
                            <el-option v-for="v in $store.state.AllUsers" v-if="v.InternshipState!=8"
                                       :label="v.UserName" :value="v.Id+''"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="付款方式" prop="PayMethod">
                        <el-select placeholder="请选择" v-model="Purchase.PayMethod" style="width: 100%">
                            <el-option :value="0" label="借款"></el-option>
                            <el-option :value="1" label="对公账户"></el-option>
                            <el-option :value="2" label="电子账户"></el-option>
                            <el-option :value="4" label="备用金"></el-option>
                            <el-option :value="8" label="垫付"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="采购金额" prop="TotalPrice">
                        <el-input disabled v-model="Purchase.TotalPrice"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="垫付人" prop="AdvanceParty">
                        <el-select placeholder="请选择" filterable
                                   :disabled="Purchase.PayMethod!=8||Purchase.PayMethod==''"
                                   v-model="Purchase.AdvanceParty" style="width: 100%">
                            <el-option v-for="v in $store.state.AllUsers" v-if="v.InternshipState!=8"
                                       :label="v.UserName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="快递公司" prop="ExpressKey">
                        <el-select v-model="Purchase.ExpressKey" filterable style="width: 100%">
                            <el-option v-for="ex in AllExpress" :value="ex.Key" :label="ex.Name"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="快递单号" prop="ExpressNumber">
                        <el-input v-model="Purchase.ExpressNumber"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="是否有发票" prop="IsInvoice">
                        <el-select v-model="Purchase.IsInvoice" filterable style="width: 100%">
                            <el-option :value="true" label="是"></el-option>
                            <el-option :value="false" label="否"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="采购类型" prop="IsInvoice">
                        <el-select placeholder="请选择" v-model="Purchase.PurchaseType" style="width: 100%">
                            <el-option label="办公用品" :value="1"></el-option>
                            <el-option label="生产原料" :value="2"></el-option>
                            <el-option label="仪器设备" :value="4"></el-option>
                            <el-option label="其他" :value="8"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col>
                    <el-form-item label="商品列表">
                        <el-table border stripe :data="tableData" ref="multipleTable"
                                  @selection-change="SelectionChange_event">
                            <el-table-column type="selection" width="40" align="center"></el-table-column>
                            <el-table-column label="名称" prop="DeviceName" align="center"
                                             show-overflow-tooltip></el-table-column>
                            <el-table-column label="规格型号" prop="DeviceModel" align="center"
                                             show-overflow-tooltip></el-table-column>
                            <el-table-column label="库存数量" prop="StockAmount" align="center"></el-table-column>
                            <el-table-column label="参考网址" prop="ReferenceWebsite" align="center"></el-table-column>
                            <el-table-column label="购买数量" prop="Amount" align="center">
                                <template slot-scope="slot">
                                    <el-input size="mini" v-model="slot.row.Amount"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column label="单位" prop="Unit" align="center"></el-table-column>
                            <el-table-column label="调研单价" prop="Unit" align="center">
                                <el-table-column label="1" prop="SurveyUnitPrice1" align="center">
                                    <template slot-scope="slot">
                                        <el-input size="mini" v-model="slot.row.SurveyUnitPrice1"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column label="2" prop="SurveyUnitPrice2" align="center">
                                    <template slot-scope="slot">
                                        <el-input size="mini" v-model="slot.row.SurveyUnitPrice2"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column label="3" prop="SurveyUnitPrice3" align="center">
                                    <template slot-scope="slot">
                                        <el-input size="mini" v-model="slot.row.SurveyUnitPrice3"></el-input>
                                    </template>
                                </el-table-column>
                            </el-table-column>
                            <el-table-column label="复核单价" prop="ReviewUnitPrice" align="center">
                                <template slot-scope="slot">
                                    <el-input size="mini" v-model="slot.row.ReviewUnitPrice"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column label="复核总价" prop="TotalPrice" align="center">
                                <template slot-scope="slot">
                                    {{ Allmoney(slot.row) }}
                                </template>
                            </el-table-column>
                            <el-table-column label="实际支付总价" prop="ActualPrice" align="center">
                                <template slot-scope="slot">
                                    <el-input size="mini" v-model="slot.row.ActualPrice"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column label="到货地址" prop="ArrivalAddress" align="center">
                                <template slot-scope="slot">
                                    <el-input size="mini" v-model="slot.row.ArrivalAddress"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column label="操作" align="center" width="178px">
                                <template slot-scope="slot">
                                    <el-button icon="el-icon-close" size="mini" type="warning"
                                               @click="ToVoid_event(slot.row)">作废
                                    </el-button>
<!--                                    <el-button icon="el-icon-delete" size="mini" type="danger"-->
<!--                                               @click="TableRemove_event(slot.row)">删除-->
<!--                                    </el-button>-->
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="用途说明" prop="PurchaseCause">
                        <el-input v-model="Purchase.PurchaseCause"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="关联项目" prop="ProjectId">
                        <el-select v-model="Purchase.ProjectId" placeholder="请选择" style="width: 100%" filterable>
                            <el-option label="无" :value="0"></el-option>
                            <el-option v-for="v in AllProject" :label="v.ProjectName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="供货商信息" prop="VendorInf">
                        <el-select v-model="Purchase.VendorInf" placeholder="请选择" style="width: 100%" filterable>
                            <el-option label="无" :value="0"></el-option>
                            <el-option v-for="v in AllCustomer" :label="v.CustomerCompany" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col align="center">
                    <el-button type="success" @click="Submit_enent">确认修改</el-button>
                    <el-button type="success" @click="AddExistingMaterial_dialog=true">添加物资</el-button>
                    <el-button type="warning" @click="PurchaseCancel_event">保存并提交审核</el-button>
                    <!--                    <el-button type="primary" @click="Purchasesplit_event">采购拆分</el-button>-->
                    <el-button type="success" @click="Purchased_event">已采购</el-button>
                    <el-button type="success" @click="Arrived_event">已到货</el-button>
                </el-col>
            </el-row>
        </el-form>
        <el-dialog :visible.sync="AddExistingMaterial_dialog" :modal="false" width="90%" top="10px" title="现有物资导入">
            <AddExistingMaterial :selection_-material="tableData" :all-device-state="AllDeviceState" :all-device-category="AllDeviceCategory" :all-department="AllDepartment"></AddExistingMaterial>
        </el-dialog>
    </div>
</template>

<script>
    import AddExistingMaterial from "../../Material/AddExistingMaterial";

    export default {
        name: "UpdatePurchase",
        components: {
            'AddExistingMaterial': AddExistingMaterial
        },
        props: {
            IsSee: {
                type: false,
                required: true,
            },
            AllDepartment: {
                type: Array,
                required: true,
            },
            AllProject: {
                type: Array,
                required: true,
            },
            AllDeviceState: {
                type: Array,
                required: true,
            },
            AllDeviceCategory: {
                type: Array,
                required: true,
            },
            AllCustomer: {
                type: Array,
                required: true,
            },
            //申购数据实体
            Purchase: {
                type: Object,
                required: true
            },
            //快递公司
            AllExpress: {
                type: Array,
                required: true,
            },
        },
        data() {
            return {
                //商品列表
                tableData: [],
                //多选列表的数据集合
                SelectMore_lis: [],
                //认证
                rules: {
                    DepartmentId: [{required: true, message: '请选择申购人员', trigger: 'change'}],
                    ApplyUserId: [{required: true, message: '请选择申购人员', trigger: 'change'}],
                    PurchaseUserId: [{required: true, message: '请选择采购人员', trigger: 'change'}],
                    PayMethod: [{required: true, message: '请选择付款方式', trigger: 'change'}],
                    IsInvoice: [{required: true, message: '请选择是否有发票', trigger: 'change'}],
                    PurchaseType: [{required: true, message: '请选择采购类型', trigger: 'change'}],

                },
                AddExistingMaterial_dialog:false
            }
        },
        created() {
            // 执行一次
            this.init(this.Purchase)
        },
        watch: {
            Purchase: function (obj) {
                this.init(obj)
            },
            tableData: function (obj) {
                let allmoney = 0
                obj.forEach(d => {
                    if (parseFloat(d.ActualPrice) > 0) {
                        allmoney += parseFloat(d.ActualPrice)
                    } else {
                        allmoney += parseFloat(d.Amount) * parseFloat(d.ReviewUnitPrice)
                    }
                })
                this.Purchase.TotalPrice = allmoney
            }
        },
        methods: {
            // 商品作废
            ToVoid_event(obj) {
                let loadding = this.openLoading();
                this.$http.post('Purchase/CancelGood', {
                    ID: obj.Id,
                    PurchaseNumber: obj.PurchaseNumber,
                }).then(res => {
                    loadding.close();
                    if (res.data.IsSuccess) {
                        this.TableRemove_event(obj)
                        this.$message.success(res.data.Msg)
                    } else {
                        this.$message.warning(res.data.Msg)
                    }
                })
            },
            //表格数据移除
            TableRemove_event(obj) {
                this.tableData.forEach((item, i) => {
                    if (item.Id == obj.Id) {
                        this.tableData.splice(i, 1)
                    }
                })
            },
            //获取表格数据
            init(obj) {
                this.$http.get('Report/GetDetialPurchaseGoods?PurchaseNumber=' + obj.PurchaseNumber).then(res => {
                    let Tbdata = []
                    res.data.forEach(d => {
                        if (!d.IsInbound) {
                            Tbdata.push(d);
                        }
                    })
                    this.tableData = Tbdata
                    let allmoney = 0
                    this.tableData.forEach(item => {
                        if (parseFloat(item.ActualPrice) > 0) {
                            allmoney += parseFloat(item.ActualPrice)
                        } else {
                            allmoney += parseFloat(item.TotalPrice)
                        }
                    })
                    this.Purchase.TotalPrice = allmoney
                })
            },
            //采购拆分
            Purchasesplit_event() {
                if (this.SelectMore_lis.length == 0) {
                    this.$message.info('您还未选择需要拆分的采购商品')
                    return
                }
                if (this.SelectMore_lis.length == this.tableData.length) {
                    this.$message.info('不能选择所有的采购商品')
                    return
                }
                let ids = []
                this.SelectMore_lis.forEach(d => {
                    ids.push(d.Id)
                })
                let loadding = this.openLoading();
                this.$http.post('Purchase/PurchaseSplit', {
                    PurchaseNumber: this.Purchase.PurchaseNumber,
                    arrDel: ids,
                }).then(res => {
                    loadding.close();
                    if (res.data.IsSuccess) {
                        this.$parent.$parent.UpdatePurchase_dialog.dialog = false
                        this.$message.success(res.data.Msg)
                        this.$parent.$parent.formdata()
                    } else {
                        this.$message.warning(res.data.Msg)
                    }
                })
            },
            //采购确认
            PurchaseCancel_event() {
                let loadding = this.openLoading();
                // this.$http.get('Purchase/PurchaseConfim?PurchaseNumber=' + this.Purchase.PurchaseNumber).then(res => {
                //     loadding.close();
                //     if (res.data.IsSuccess) {
                //         this.$message.success(res.data.Msg)
                //         this.$parent.$parent.formdata()
                //         this.$parent.$parent.UpdatePurchase_dialog.dialog = false
                //     } else {
                //         this.$message.warning(res.data.Msg)
                //     }
                // })
                this.$http.post("Purchase/SubmitPurchaseContent", {
                    purchase: this.Purchase,
                    addPurchase: this.tableData
                }).then(res => {
                    loadding.close();
                    if (res.data.IsSuccess) {
                        this.$message.success('提交成功')
                        this.$parent.$parent.formdata()
                        this.$parent.$parent.UpdatePurchase_dialog.dialog = false
                    } else {
                        this.$message.warning(res.data.Msg)
                    }
                }).catch(() => {
                    loadding.close();
                    this.$message.error('服务器异常,请联系管理员!')
                })
            },
            //获取新建的物资
            GetNewDevice(NewDevice_) {
                NewDevice_.DeviceId = NewDevice_.Id
                NewDevice_.ActualPrice=0
                this.tableData.push(NewDevice_)
            },
            //修改提交
            Submit_enent() {
                this.$refs['UpdataApplyPurchase_refs'].validate(vali => {
                    if (vali) {
                        if (this.Purchase.PurchaseUserId == '') {
                            this.$message.warning('数据信息不完整!')
                        } else {
                            let result = true
                            for (let i = 0; i < this.tableData.length; i++) {
                                if (this.tableData[i].Amount === "" || this.tableData[i].ReviewUnitPrice === "" || (this.tableData[i].TotalPrice === "" && this.tableData[i].ActualPrice === "")) {
                                    result = false;
                                    this.$message.warning("商品关键信息不能为空！");
                                    return false;
                                }
                            }
                            if (result) {
                                let loadding = this.openLoading();
                                this.$http.post('Purchase/UpdatePurchaseContent', {
                                    purchase: this.Purchase,
                                    addPurchase: this.tableData,
                                }).then(res => {
                                    loadding.close();
                                    if (res.data.IsSuccess) {
                                        this.$message.success('修改成功')
                                        this.tableData = []
                                        this.$parent.$parent.formdata()
                                        this.$parent.$parent.UpdatePurchase_dialog.dialog = false
                                    } else {
                                        this.$message.warning('修改失败!')
                                    }
                                })
                            }
                        }
                    }
                })
            },
            //单个物品总价
            Allmoney(row) {
                let money = 0
                this.tableData.forEach(item => {
                    if (parseFloat(item.ActualPrice) > 0) {
                        money += parseFloat(item.ActualPrice)
                    } else {
                        money += (parseFloat(item.Amount) * parseFloat(item.ReviewUnitPrice))
                    }
                })
                this.Purchase.TotalPrice = money.toFixed(2)
                if (isNaN(row.Amount * row.ReviewUnitPrice)) {
                    return ''
                } else {
                    return (row.Amount * row.ReviewUnitPrice).toFixed(2)
                }
            },
            //表格数据选择事件
            SelectionChange_event(value) {
                this.SelectMore_lis = value
            },
            //已采购
            Purchased_event() {
                this.$http.get('Purchase/UpdataPurchasedStatePurchased?purchaseId=' + this.Purchase.Id).then(res => {
                    if (res.data.IsSuccess) {
                        this.$message.success(res.data.Msg)
                        this.tableData = []
                        this.$parent.$parent.formdata()
                        this.$parent.$parent.UpdatePurchase_dialog.dialog = false
                    } else {
                        this.$message.warning(res.data.Msg)
                    }
                })
            },
            //已到货
            Arrived_event() {
                this.$http.get('Purchase/UpdataPurchasedStateArrived?purchaseId=' + this.Purchase.Id).then(res => {
                    if (res.data.IsSuccess) {
                        this.$message.success(res.data.Msg)
                        this.tableData = []
                        this.$parent.$parent.formdata()
                        this.$parent.$parent.UpdatePurchase_dialog.dialog = false
                    } else {
                        this.$message.warning(res.data.Msg)
                    }
                })
            },
        }
    }
</script>

<style scoped>

</style>
